//////////////////////////////////////////////////////////////////////////
///  Copyright, (c) Shanghai United Imaging healthcare Inc., 2011
///  All rights reserved.
///
///  \author  Huang Yonggang  yonggang.huang@united-imaging.com
///
///  \file    main.c
///  \brief   main
///
///  \version 1.0
///  \date    2015/04/27
//////////////////////////////////////////////////////////////////////////
#include <windows.h>

#include "public.h"
#include <stdio.h>
#include "ut.h"
#ifdef _MSC_VER
extern void process_line(unsigned char* line); 
#else
#include <unistd.h>
#include <stdbool.h>
#include <pthread.h>
extern void ipc_cliserv_task(void *arg);
#endif

//extern 
extern void register_app_cmds(void);

HANDLE hDriver[UIH_CND_FPGA_NUMBER]={NULL};
int MAX_FILE_NUM = 100;
ULONG ulFrameSize = DMAOUTCONTIGUOUSMEMORYBLOCKSIZE ;

const char* g_raw_data_path = "D:\\Cnd_Driver\\dataRaw";


int main(int argc, char** argv)
{
    if (argc > 1)
    {
        g_raw_data_path = argv[1];
        printf("new output path:%s\n", g_raw_data_path);
    }

    int index;
    #ifdef _MSC_VER
    char line[256];
    char* dbg_prompt_s = (char*) "CNDemo> ";
    memset(line, 0, sizeof(line));
    #else
    char *ipc_path = "../hc_debug.ipc";
    int tid = 0;
    pthread_create(&tid, NULL, ipc_cliserv_task, (void*)ipc_path);
    #endif
    system("title US");
    register_app_cmds();

    //inital hDriver[UIH_CND+FPGA_NUMBER]
    for (index=0;index<UIH_CND_FPGA_NUMBER;index++)
    {
        hDriver[index] = NULL;
    }

    while(1)
    {
        #ifdef _MSC_VER
        printf((char*) dbg_prompt_s);
        fflush(stdout);
        if (gets((char*)line) < 0)
            break;  /* in target, there is a different way ? */
        if ((line[0] == 0) || (line[0] == '\n'))
            continue;
        process_line(line);
        #else
        usleep(1000);
        #endif
    }
    for (index=0;index<UIH_CND_FPGA_NUMBER;index++)
    {
        if ((NULL != hDriver[index])&&(hDriver[index] != INVALID_HANDLE_VALUE))
        {
            printf("Close the device handle \n");
            CloseHandle(hDriver[index]);
        }
    }
    return 1;
}

